import React from 'react';
import {sessionManager} from '../../utils/sessionStorage';
import { withRouter } from 'react-router-dom'
import {Form ,Row ,Col, Select ,Button ,DatePicker ,Input } from 'antd';
import Grid from '../../components/table/grid';
import {injectIntl} from 'react-intl';
import axios from "axios/index";
import qs from 'qs';
import moment from 'moment';

const FormItem = Form.Item;
const createForm = Form.create;
const Option = Select.Option;
const { RangePicker } = DatePicker;

@withRouter
class Tax extends React.Component {
    constructor(props) {
        super(props);
        this.common = this.props.intl.messages.common;
        this.state={
            params:{
                invoiceMonthBegin:moment().format('YYYYMMDD'),
                invoiceMonthEnd:moment().format('YYYYMMDD')
            },
            refresh:0
        };
        this.columns = [
            {
                title: '处理状态',
                key: 'status',
                render: (text, record) => {
                    if(text.status==0){
                        return '未处理'
                    }
                    if(text.status==1){
                        return '发票'
                    }
                    if(text.status==2){
                        return '抵扣'
                    }
                    if(text.status==3){
                        return '免收'
                    }
                },
            },
            {
                title: '开票日期',
                dataIndex: 'invoiceMonth',
                key: 'invoiceMonth',
            },
            {
                title: '商户名称',
                dataIndex: 'billUserName',
                key: 'billUserName'
            },
            {
              title: '商户账号',
              dataIndex: 'billUserAccount',
              key: 'billUserAccount'
            },
            {
                title: '开票总额',
                dataIndex: 'invoiceAmount',
                key: 'invoiceAmount',
            },
            {
                title: '税点总额',
                dataIndex: 'invoiceTax',
                key: 'invoiceTax',
            },
            {
                title: '操作',
                render:(text,record)=>(
                    <a onClick={(()=>{this.goLink(text,record)})}>开票详情</a>
                )
            },
        ]
    }


    goLink=(text,record)=>{
        sessionManager.set('tax',text);
        this.props.history.push({pathname: '/finance/TaxDetails'});
    }

    handleSubmit = (e) => {
        e.preventDefault();
        this.props.form.validateFieldsAndScroll((err, values) => {
            if (!!err) {
                return;
            }
            console.log(values);
            values.invoiceMonthBegin=values.invoiceMonth&&values.invoiceMonth.length>0?values.invoiceMonth[0].format('YYYYMMDD'):undefined;
            values.invoiceMonthEnd=values.invoiceMonth&&values.invoiceMonth.length>0?values.invoiceMonth[1].format('YYYYMMDD'):undefined;
            delete values.invoiceMonth;
            this.setState(Object.assign(this.state.params,values));
            this.setState({
                refresh:this.state.refresh+1
            })
        });
    };


    render() {
        const token=(JSON.parse(sessionStorage.getItem("token"))||{}).token;
        let locale=sessionManager.get('locale');
        let exportQs=`${qs.stringify(this.state.params)}&token=${token}&locale=${locale}`;
        console.log('exportQs',exportQs);
        const {getFieldDecorator} = this.props.form;
        return (
            <div>
                <form form={this.props.form}>
                    <Row gutter={24} type="flex">
                        <Col xl={6} xxl={4}>
                            <FormItem
                            >
                                {getFieldDecorator('invoiceMonth',{initialValue:[moment(),moment()]})(
                                    <RangePicker/>
                                )}
                            </FormItem>
                        </Col>
                        <Col xl={4} xxl={4}>
                            <FormItem
                            >
                                {getFieldDecorator('billUserName',)(
                                    <Input placeholder={'商户名称'}/>
                                )}
                            </FormItem>
                        </Col>
                        <Col xl={6} xxl={4}>
                            <FormItem
                            >
                                {getFieldDecorator('billUserAccount',)(
                                    <Input placeholder={'商户账号'}/>
                                )}
                            </FormItem>
                        </Col>
                        <Col span={3}>
                            <FormItem
                            >
                                {getFieldDecorator('status',)(
                                    <Select placeholder={'处理方式'} allowClear={true}>
                                        <Option value="0">未处理</Option>
                                        <Option value="1">发票</Option>
                                        <Option value="2">抵扣</Option>
                                        <Option value="3">免收</Option>
                                    </Select>
                                )}
                            </FormItem>
                        </Col>
                        <Col>
                            <FormItem>
                                <Button type="primary" htmlType="submit"
                                        onClick={this.handleSubmit} className="ant-btn-84">{this.common.search}</Button>
                                <a href={`${axios.defaults.baseURL}/api/settlement/invoice/taxList/export?${exportQs}`}>
                                    <Button type="primary">{this.common.exportStatement}</Button>
                                </a>
                            </FormItem>
                        </Col>
                    </Row>
                </form>
                <Grid columns={this.columns} params={this.state.params}
                      url={'/api/settlement/invoice/taxList'} refresh={this.state.refresh}/>
            </div>
        )
    }
}

Tax = createForm()(Tax);
export default injectIntl(Tax)
